package com.wu.web;

import com.wu.bean.User;
import com.wu.service.UserService;
import com.wu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/UpdateUserPassword")
public class UpdateUserPasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String OriginalPassword=req.getParameter("OriginalPassword");
        String NewPassword=req.getParameter("NewPassword");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session=req.getSession();
        String UserName=session.getAttribute("username").toString();
        //先查询原密码是否正确
        UserService userService=new UserServiceImpl();
        User user =userService.Login(new User(null,UserName,null,OriginalPassword,null,null,null,null,null));
        if(user!=null)
        {
            int ok=userService.UpdateUserPassword(UserName,NewPassword);
            if(ok>0)
            {
                session.setAttribute("username","");
                resp.getWriter().print("ok");
            }else
            {
                resp.getWriter().print("修改失败");
            }
        }else
        {
            resp.getWriter().print("原密码错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
